package org.onvivals.admin.module.pms.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.pms.entity.PmsQuestionAnswer;
import org.onvivals.module.pms.param.PmsQuestionAnswerParam;
import org.onvivals.module.pms.service.IPmsQuestionAnswerService;
import org.onvivals.module.ums.entity.UmsUser;
import org.onvivals.module.ums.service.IUmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * QuestionSquareController
 *
 * @author weirdor (179946422@qq.com)
 * @version v1.0.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/question")
public class QuestionSquareController {

	private final IPmsQuestionAnswerService questionAnswerService;


	private final IUmsUserService memberService;

	@GetMapping
	public Result<PageResult<PmsQuestionAnswer>> list(PmsQuestionAnswerParam param) {
		PageParam<PmsQuestionAnswer, PmsQuestionAnswerParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
		IPage<PmsQuestionAnswer> pa = questionAnswerService.page(page, page.getWrapper());
		pa.getRecords().forEach(k -> {
			Long uid = k.getUid();
			if (ObjectUtil.isNotEmpty(uid)) {
				UmsUser member = memberService.getById(uid);
				if (ObjectUtil.isNotEmpty(member)) {
					k.setMember(member);
				}
			}
		});
		PageResult<PmsQuestionAnswer> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
		return Result.OK(pageResult);
	}

	@DeleteMapping("/{id}")
	public Result<?> remove(@PathVariable("id") Long id) {
		if (questionAnswerService.removeById(id)) {
			return Result.OK("删除成功");
		}
		return Result.error("删除失败");
	}
}
